//
//  PHTabBar.swift
//  PHWeibo
//
//  Created by 潘荟 on 16/7/19.
//  Copyright © 2016年 BAT. All rights reserved.
//

import UIKit

class PHTabBar: UITabBar {
    
    var closure:(()->())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    //实现监听方法
    func composeClick() {
        closure?()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        
        addSubview(compostBtn)
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let width = SCREENW * 0.2
        var index:CGFloat = 0
        
        //遍历子控件
        for value in subviews {
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                value.frame.origin.x = width * index
                value.frame.size.width = width
                
                index++
                if index == 2 {
                    index++
                }
            }
            
        }
        
        compostBtn.center.x = frame.width * 0.5
        compostBtn.center.y = frame.height * 0.5
    }
    
    //懒加载子控件
    lazy var compostBtn:UIButton = {
        let btn = UIButton()
        
        btn.addTarget(self, action: "composeClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.sizeToFit()
        return btn
    }()

}
